﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Studio77.CrazyJigsaw.Core;
using Studio77.CrazyJigsaw.Util;

namespace Studio77.CrazyJigsaw
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            try
            {
                int count = NavigationService.BackStack.Count();
                if (count > 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        NavigationService.RemoveBackEntry();
                    }
                }
            }
            catch
            { }
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= new RoutedEventHandler(MainPage_Loaded);
            //this.StoryboardTabTip.RepeatBehavior = RepeatBehavior.Forever;
            //this.StoryboardTabTip.Begin();

            this.StoryboardStartBtn.RepeatBehavior = RepeatBehavior.Forever;
            this.StoryboardStartBtn.Begin();

            this.btnStart.Click += new RoutedEventHandler(btnStart_Click);
            this.btnContinue.Click += new RoutedEventHandler(btnContinue_Click);

            this.btnSoundOff.Click += new RoutedEventHandler(btnSoundOff_Click);
            this.btnSoundOn.Click += new RoutedEventHandler(btnSoundOn_Click);

            if (!StageManager.Instance.IsPlaySound)
            {
                this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
                this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
            }

            //播放音效
            AudioPlayHelper.PlayBackground("background.mp3");
        }

        void btnSoundOn_Click(object sender, RoutedEventArgs e)
        {
            StageManager.Instance.IsPlaySound = false;
            AudioPlayHelper.StopBackground();
            this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
            this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
        }

        void btnSoundOff_Click(object sender, RoutedEventArgs e)
        {
            StageManager.Instance.IsPlaySound = true;
            AudioPlayHelper.PlayBackground("background.mp3");
            this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
            this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
        }

        void btnContinue_Click(object sender, RoutedEventArgs e)
        {
            //播放音效
            SoundManager.PlayEffect(1);
            if (!String.IsNullOrEmpty(StageManager.Instance.LastStage))
            {
                NavigationService.Navigate(new Uri("/Pages/GamePage.xaml?StageCode=" + StageManager.Instance.LastStage, UriKind.Relative));
            }
        }

        void btnStart_Click(object sender, RoutedEventArgs e)
        {
            //播放音效
            SoundManager.PlayEffect(1);
            this.StoryboardStartBtn.Stop();
            //跳转到关卡选择界面
            NavigationService.Navigate(new Uri("/Pages/ClassifyPage.xaml", UriKind.Relative));
        }
    }
}